package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.SkillVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.cache.MonsterServiceCache;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.message.client2server.game.Game;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-06-09 17:38
 **/
public class CGameSkillHandler extends MsgHandler {
    private static final Logger logger = LoggerFactory.getLogger("CGameSkillHandler.class");
    private RoleServiceCache roleServiceCache;
    private RouteUtils routeUtils;
    private MonsterServiceCache monsterServiceCache;
    private UserServiceCache userServiceCache;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameSkillHandler(MessageVO messageVO) {
        super(messageVO);
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.monsterServiceCache = SpringContextHolder.getBean("monsterServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        Game.CGameSkillMsg cGameSkillMsg = (Game.CGameSkillMsg) messageVO.getMessage();
        int code = cGameSkillMsg.getCode();
        List<String> idList = new ArrayList<>();
        for (GameEntity.stringMsg stringMsg : cGameSkillMsg.getSenderIdList()) {
            idList.add(stringMsg.getStringId());
        }
        getSkill(idList);
    }

    public void getSkill(List<String> idList) {
        routeUtils.sendGameSkillResponse(Common.GAME_GET_SKILL_SUCCESS, "",
                messageVO.getUserId(), messageVO.getNetId(), getAllRoleIdToSkillVOMap(idList));
    }

    public Map<String, Map<String, SkillVO>> getAllRoleIdToSkillVOMap(List<String> idList) {
        Map<String, Map<String, SkillVO>> allRoleIdToSkillVOMap = new HashMap<>();
        for (String id : idList) {
            /**
             * 判断是获取角色技能还是boss技能，做对应返回
             */
            if (userServiceCache.getUserServiceMap().containsKey(id)) {
                List<String> roleIdList = userServiceCache.getUserServiceMap().get(id).getRoleIdList();
                for (String roleId : roleIdList) {
                    allRoleIdToSkillVOMap.put(roleId,
                            roleServiceCache.getRoleServiceMap().get(roleId).getSkillVOMap());
                }
            } else if (monsterServiceCache.getMonsterServiceMap().containsKey(id)) {
                allRoleIdToSkillVOMap.put(id,
                        monsterServiceCache.getMonsterServiceMap().get(id).getSkillVOMap());
            }

            logger.info(id + ": 实体获取技能成功。");
        }
        return allRoleIdToSkillVOMap;
    }
}
